var _grid = $('#jgTable'); 
gridWidth = $('.u_toolbar');

$(function(){

    _gridInit();

    $('li[data-opt="add"]').on('click', addCode);
    $("li[data-opt='edit']").on('click', function(){
        var rowId = _grid.jqGrid('getGridParam', 'selrow');
        editCode(rowId);
    });
    $("li[data-opt='delete']").on('click', function(){
        var rowId = _grid.jqGrid('getGridParam', 'selrow');
        deleteCode(rowId);
    });
    $("li[data-opt='u_save']").on('click', function(){  //MDM0017/MDM001708
        var rowData = _grid.jqGrid('getRowData')
        var contentData = [];
        rowData.forEach(function(v,i,a) {
            contentData.push({DocEntry:v.DocEntry, OrderID:i+1});
        });
        var requestData = {
            Content: contentData
        }
        for(i in postData){
            requestData[i] = postData[i]
        }
        $.ajax({
            type: 'post',
            url: mdmPath + '/MDM0017/MDM001708',
            data: requestData,
            dataType: 'json',
            success: function(re) {
                if(re.Status==200) {
                    myAlert('保存成功');
                } else {
                    myAlert(re.Message);
                }
            }
        })
    });
    $("li[data-opt='opt_search']").find(">i").on('click', function(){
        gridSearch.Content = {
            SearchText: $("#opt_search").find(">input").val()
        };
        search();
    });

});

function addCode(){
    window.open('MDM001702.html', '_blank');  
}

function editCode(rowId){
    if(rowId){  //判断是否选中有效行
        var rowData = _grid.jqGrid('getRowData', rowId);
        window.open('MDM001702.html?DocEntry=' + rowData.DocEntry, '_blank');
    }else{
        alert('请选择您要编辑的数据');
    }   
}

function deleteCode(rowId){
    if(rowId){  //判断是否选中有效行

    }else{
        alert('请选择您要删除的数据');
    }   
}

function alarmFormatter(cellvalue, options, rowdata){
    var grid = $(this);
    if(JSON.stringify(rowdata)!=='{}'){
        cellvalue = '<a href="javascript:0" opt-data="'+grid[0].id+'_edit" title="编辑"><i class="fa fa-pencil"></i></a>'+
            '&nbsp;<a href="javascript:0" opt-data="'+grid[0].id+'_delete" title="删除"><i class="fa fa-trash-o"></i></a>';
    }else{
        cellvalue = '';
    }
    return cellvalue;
}
function _gridInit(){
    var _gridData = Object.create(gridData);
    _gridData.url = mdmPath + '/MDM0017/MDM001702';
    _gridData.colModel = [
        {label:'单号', name:'DocEntry'},
        {label:'标题', name:'Title'},
        {label:'类别', name:'Category'},
        {label:'操作', name:'Options', width:80, align:'center', formatter: alarmFormatter, sortable:false}
    ];
    _gridData.sortname = 'OrderID';
    _gridData.rowNum = '100';
    var _postData = {
        Method: 'Q',
        Content:{
            SearchText: ''
        }
    };
    for(i in postData){
        _postData[i] = postData[i];
    }
    //console.log(postData)
    _gridData.beforeRequest = function(){
        _gridData.grid.jqGrid("setGridParam", {
            postData : _postData,
        });
    };
    _gridData.loadComplete = function(xhr){
        xhr.Status == -999 && alert_QX($('body'), xhr.Message);
        $('a[opt-data="jgTable_edit"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            editCode(rowId);
        });
        $('a[opt-data="jgTable_delete"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            deleteCode(rowId);
        });
        _gridData.grid.jqGrid('sortableRows',{
            update:function(ev,ui){
                var item = ui.item[0],
                ri = item.rowIndex,
                itemId = item.id;
            }
        });
    };
    _gridData.ondblClickRow = function(rowid,iRow,iCol,e){
        editCode(rowid);
    };
    gridInit(_gridData);
    gridSearch = _postData;
}
